//
//  AdViewController.m
//  admobandiad
//
//  Created by apple on 11-4-11.
//  Copyright 2011 IFunSoft. All rights reserved.
//

#import "AdViewController.h"
#import "cocos2d.h"

#import "AdMobView.h"

@implementation AdViewController

@synthesize delegate;
@synthesize isRemoveAd;


static bool isRemoveAd=false; //类的静态变量

- (id) initWithMasterView:(UIView*) newMasterView
{

	
	if( nil != (self = [super init]) )
	{
		self.view = newMasterView;
		
		//Initialize the class manually to make it compatible with iOS < 4.0
		Class classAdBannerView = NSClassFromString(@"ADBannerView");
		if (classAdBannerView != nil)
		{
			adBannerView = [[classAdBannerView alloc] init];
			[adBannerView setDelegate:self];
			[adBannerView setRequiredContentSizeIdentifiers: [NSSet setWithObjects:
															  ADBannerContentSizeIdentifier320x50,
															  ADBannerContentSizeIdentifier320x50, nil]];
			/*
			[adBannerView setRequiredContentSizeIdentifiers: [NSSet setWithObjects:
															  ADBannerContentSizeIdentifierPortrait,
															  ADBannerContentSizeIdentifierLandscape, nil]];
			 */
			
			
			[self.view addSubview:adBannerView];
			[adBannerView setHidden:YES];
			
			[self updateBannerViewOrientation];
			
		}
		else
		{
			//Request an AdMob Ad
			if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
				adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_748x110 withDelegate:self];
			else
				adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_320x48 withDelegate:self];
			[adMobAd retain];
		}
		
		
		
	}
	

	
	return self;
}

- (UIDeviceOrientation)currentOrientation
{
	return [[CCDirector sharedDirector] deviceOrientation];
}

- (void)updateBannerViewOrientation
{
	[self rotateBannerView:[self currentOrientation]];
}

- (void)rotateBannerView:(UIDeviceOrientation)toDeviceOrientation
{
	//Get Screen Size
	CGSize screenSize = [[UIScreen mainScreen] bounds].size;
	
	if (adBannerView)
	{
		if (UIDeviceOrientationIsLandscape(toDeviceOrientation))
			[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
		else
			[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
		
		//Get ADBannerView Frame
		CGSize adBannerViewSize = [adBannerView frame].size;
		
		[(UIView*)adBannerView setTransform:CGAffineTransformIdentity];
//		[adBannerView setFrame:CGRectMake(0.f, 0.f, adBannerViewSize.width, adBannerViewSize.height)];
		[adBannerView setFrame:CGRectMake(100.f, 200.f, adBannerViewSize.width, adBannerViewSize.height)];
		
//		NSLog(@"-----------------%d",toDeviceOrientation);
		
		//Set the transformation for each orientation
		switch (toDeviceOrientation)
		{
			case UIDeviceOrientationPortrait:
			{
				[adBannerView setCenter:CGPointMake(screenSize.width/2, screenSize.height - adBannerViewSize.height/2)];
				if ([adBannerView isHidden])
					[adBannerView setCenter:CGPointMake(screenSize.width/2, screenSize.height + adBannerViewSize.height/2)];
			}
				break;
			case UIDeviceOrientationPortraitUpsideDown:
			{
				[(UIView*)adBannerView setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(180))];
				[adBannerView setCenter:CGPointMake(screenSize.width/2, adBannerViewSize.height/2)];
				if ([adBannerView isHidden])
					[adBannerView setCenter:CGPointMake(screenSize.width/2, -adBannerViewSize.height/2)];
				
			}
				break;
			case UIDeviceOrientationLandscapeLeft:
			{
				
				
				[(UIView*)adBannerView setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90))];
				//[adBannerView setCenter:CGPointMake(adBannerViewSize.height/2+65, screenSize.height/2)];
				[adBannerView setCenter:CGPointMake(adBannerViewSize.height/2+0   //中心点x居下
													, screenSize.height/2)]; //中心点y水平居中
				
				//[adBannerView setCenter:CGPointMake(screenSize.width/2+0   //中心点x居中
				//									, screenSize.height/2)]; //中心点y水平居中

				
				if ([adBannerView isHidden]){
					
					[adBannerView setCenter:CGPointMake(screenSize.width/2+0   //中心点x居中
														, screenSize.height/2)]; //中心点y水平居中
				}
			}
				
				break;
			case UIDeviceOrientationLandscapeRight:
			{
				[(UIView*)adBannerView setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(-90))];
				[adBannerView setCenter:CGPointMake(screenSize.width - adBannerViewSize.height/2, screenSize.height/2)];
				if ([adBannerView isHidden])
					[adBannerView setCenter:CGPointMake(screenSize.width + adBannerViewSize.height/2, screenSize.height/2)];
				
			}
				break;
			default:
				break;
		}
	}
	
	if (adMobAd)
	{
		[adMobAd setTransform:CGAffineTransformIdentity];
		
		CGSize adMobAdSize = adMobAd.frame.size;
		
		//Set the transformation for each orientation
//		NSLog(@"-----------------%d",toDeviceOrientation);
		switch (toDeviceOrientation)
		{
			case UIDeviceOrientationPortrait:
			{
				[adMobAd setCenter:CGPointMake(screenSize.width/2, screenSize.height - adMobAdSize.height/2)];
			}
				break;
			case UIDeviceOrientationPortraitUpsideDown:
			{
				[adMobAd setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(180))];
				[adMobAd setCenter:CGPointMake(screenSize.width/2, adMobAdSize.height/2)];
			}
				break;
			case UIDeviceOrientationLandscapeLeft:
			{
				[adMobAd setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90))];
	//			[adMobAd setCenter:CGPointMake(-adMobAdSize.height/2, screenSize.height/2)];
				
				
				
				//[adMobAd setCenter:CGPointMake(screenSize.width-adMobAdSize.height/2, screenSize.height/2)]; //横屏上面
				
				//[adMobAd setCenter:CGPointMake(screenSize.width/2, screenSize.height/2)];   //横屏中间
				
				[adMobAd setCenter:CGPointMake(adMobAdSize.height/2, screenSize.height/2)];   //横屏下面
			}
				break;
			case UIDeviceOrientationLandscapeRight:
			{
				[adMobAd setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(-90))];
				[adMobAd setCenter:CGPointMake(screenSize.width - adMobAdSize.height/2, screenSize.height/2)];  //横屏下面
			}
				break;
			default:
				break;
		}
	}
}

#pragma mark -
#pragma mark ADBannerViewDelegate

- (BOOL)allowActionToRun
{
	return YES;
}

- (void) stopActionsForAd
{
	//Stop Background Music and other actions...
	
	[adBannerView setHidden:YES];
	
	//Stop Director
	[[CCDirector sharedDirector] stopAnimation];
	[[CCDirector sharedDirector] pause];
}

- (void) startActionsForAd
{
	//Stort Background Music and other actions...
	
	[adBannerView setHidden:NO];
	[self rotateBannerView:[self currentOrientation]];
	[[UIApplication sharedApplication] setStatusBarOrientation:(UIInterfaceOrientation)[self currentOrientation]];
	
	//Resume Director
	[[CCDirector sharedDirector] stopAnimation];
	[[CCDirector sharedDirector] resume];
	[[CCDirector sharedDirector] startAnimation];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
	BOOL shouldExecuteAction = [self allowActionToRun];
    if (!willLeave && shouldExecuteAction)
    {
		[self stopActionsForAd];
    }
    return shouldExecuteAction;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
	CCLOG(@"iAd Did Load");
	
	if (adMobAd)
	{
		[adMobAd setDelegate:nil];
		[adMobAd removeFromSuperview];
		[adMobAd release];
		adMobAd = nil;
	}
	
	if (![self.view.subviews containsObject:adBannerView])
		[self.view addSubview:adBannerView];
	
	[adBannerView setHidden:NO];
	[self rotateBannerView:[self currentOrientation]];
	
	[delegate adController:self didLoadiAd:banner];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
	CCLOG(@"iAd Failed To Load With Error: %@", [error localizedDescription]);
	
	[adBannerView setHidden:YES];
	
	if ([self.view.subviews containsObject:adBannerView])
		[adBannerView removeFromSuperview];
	
	if (!adMobAd)
	{
		CCLOG(@"Loading AdMob Ad...");
		if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
			adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_748x110 withDelegate:self];
		else
			adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_320x48 withDelegate:self];
		[adMobAd retain];
	}
	
	[delegate adController:self didFailedToRecieveiAd:banner];
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
	[self startActionsForAd];
}

#pragma mark -
#pragma mark AdMobDelegate

/*
 guess the beauty finger   编辑
 站点 URL： http://
 发布者 ID: a14e86f303b437c  |  获取发布者代码
 
 
 */
- (NSString *)publisherIdForAd:(AdMobView *)adView
{
	return @"a14e86f303b437c"; // this should be prefilled; if not, get it from http://www.admob.com
}


- (UIViewController *)currentViewControllerForAd:(AdMobView *)adView {
	return self;
}

- (UIColor *)adBackgroundColorForAd:(AdMobView *)adView
{
	return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (UIColor *)primaryTextColorForAd:(AdMobView *)adView
{
	return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (UIColor *)secondaryTextColorForAd:(AdMobView *)adView
{
	return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

//- (NSArray *)testDevices
//{
//	return [NSArray arrayWithObjects:
//			ADMOB_SIMULATOR_ID,								//iPhone Simulator
//			nil];
//}

- (void)didReceiveAd:(AdMobView *)adView
{
	CCLOG(@"AdMob: Did receive ad");
	
	[self rotateBannerView:[self currentOrientation]];
	[self.view addSubview:adMobAd];
	[self.view sendSubviewToBack:adMobAd];
	
	[delegate adController:self didLoadAdMobAd:adView];
}

// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView
{
	CCLOG(@"AdMob: Did fail to receive ad");
	[adMobAd removeFromSuperview];
	[adMobAd release];
	adMobAd = nil;
	
	[delegate adController:self didFailedToRecieveAdMobAd:adView];
}

- (void) stopActionsForAdMobAd
{
	//Stop Background Music and other actions...
}

- (void) startActionsForAdMobAd
{
	//Start Background Music and other actions...
}

- (void)didPresentFullScreenModalFromAd:(AdMobView *)adView
{
	[self stopActionsForAdMobAd];
}

- (void)willDismissFullScreenModalFromAd:(AdMobView *)adView
{
	[self startActionsForAdMobAd];
}

- (void)didDismissFullScreenModalFromAd:(AdMobView *)adView
{
	[self rotateBannerView:[self currentOrientation]];
	[[UIApplication sharedApplication] setStatusBarOrientation:(UIInterfaceOrientation)[self currentOrientation]];
}

#pragma mark -
#pragma mark Memory Management

- (void) dealloc
{
	if (adMobAd)
	{
		[adMobAd setDelegate:nil];
		[adMobAd removeFromSuperview];
		[adMobAd release];
		adMobAd = nil;
	}
	
	if (adBannerView)
	{
		[adBannerView setDelegate:nil];
		[adBannerView removeFromSuperview];
		[adBannerView release];
		adBannerView = nil;
	}
	
    [super dealloc];
}



#pragma mark -
#pragma mark control ad show or not

/*
+(void) removeAllAD
{
	if (adBannerView) {
		[adBannerView setHidden:YES];
		[adBannerView setDelegate:nil];
		
		if ([self.view.subviews containsObject:adBannerView]){
			[adBannerView removeFromSuperview];
			
		}
		
		[adBannerView setDelegate:nil];
		[adBannerView removeFromSuperview];
		[adBannerView release];
		adBannerView = nil;
		
	}
	
	
	
	
	if (adMobAd)
	{
		[adMobAd setDelegate:nil];
		[adMobAd removeFromSuperview];
		[adMobAd release];
		adMobAd = nil;
	}
	
	
}
*/


+(void) setRemoveAdState:(bool)remove
{
	isRemoveAd=remove;

	/*
	CCLOG(@"setRemoveAdState");
	if (remove) {
		if ([self respondsToSelector:@selector(removeAllAD)]) 
		{
			CCLOG(@"self removeAllAD true");
			[self removeAllAD];
		}
		
	}
	else {
		
	}
	 */
	
}




+(bool) getRemoveAdStatus
{
	return isRemoveAd;
}


@end